#include "StaticText.h"
#include <algorithm>

void CStaticText::SetInnerText(string str)
{
    this->_innerText.clear();
    this->_innerText.push_back(string());
    for (auto& c : str)
    {
        if (c == '\n')
        {
            this->_innerText.push_back(string());
            continue;
        }
        if (isgraph(c) || c == ' ' || c == '\t')
            this->_innerText.rbegin()->push_back(c);
    }
    this->uiupd();
}

void CStaticText::SetInnerText(vector<string> strs)
{
    this->_innerText.clear();
    this->_innerText.resize(strs.size());
    for (int i = 0; i < strs.size(); i++)
        for (auto c : strs[i])
            if (isgraph(c) || c == ' ' || c == '\t')
                this->_innerText[i].push_back(c);
    this->uiupd();
}

bool CStaticText::format(Format& painter, unsigned short& j, string str)
{
    if (j == str.size() - 1)
    {
        return true;
    }
    else
    {
        switch (str[j + 1])
        {
        case 'B':
            painter.bold = true;
            j++;
            break;
        case 'b':
            painter.bold = false;
            j++;
            break;
        case 'U':
            painter.udl = true;
            j++;
            break;
        case 'u':
            painter.udl = false;
            j++;
            break;
        case 'C':
            j++;
            if (j + 6 >= str.size())return false;
            j++;
            painter._BgR = (hextoi(str[j]) << 4) | hextoi(str[j + 1]);
            j += 2;
            painter._BgG = (hextoi(str[j]) << 4) | hextoi(str[j + 1]);
            j += 2;
            painter._BgB = (hextoi(str[j]) << 4) | hextoi(str[j + 1]);
            j++;
            painter._BgC = NC;
            break;
        case 'c':
            j++;
            if (j + 1 >= str.size())return false;
            j++;
            painter._BgC = hextoi(str[j]);
            painter._BgR = painter._BgG = painter._BgB = NC;
            break;
        case 'F':
            j++;
            if (j + 7 >= str.size())return false;
            j++;
            painter._FgR = (hextoi(str[j]) << 4) | hextoi(str[j + 1]);
            j += 2;
            painter._FgG = (hextoi(str[j]) << 4) | hextoi(str[j + 1]);
            j += 2;
            painter._FgB = (hextoi(str[j]) << 4) | hextoi(str[j + 1]);
            j++;
            painter._FgC = NC;
            break;
        case 'f':
            j++;
            if (j + 1 >= str.size())return false;
            j++;
            painter._FgC = hextoi(str[j]);
            painter._FgR = painter._FgG = painter._FgB = NC;
            break;
        default:return true;
        }
    }
    return false;
}

void CStaticText::uiupd()
{
    Format painter = this->_default;
    for (unsigned short i = 0; i < std::min<size_t>(this->_size.Y, this->_innerText.size()); i++)
    {
        for (unsigned short j = 0, k = 0; j < this->_innerText[i].size() && k < this->_size.X; j++)
        {
            if (this->_innerText[i][j] == '$')
            {
                while (j + 1 < this->_innerText[i].size() && this->_innerText[i][j + 1] != ';')
                {
                    bool p = format(painter, j, this->_innerText[i]);
                    if (p)
                    {
                        this->_UIBuf->at({ k++, i }) = { '$', painter };
                        break;
                    }
                }
                j++;
            }
            else
                this->_UIBuf->at({ k++, i }) = { this->_innerText[i][j], painter };
        }
    }
    this->_UIBuf->Update();
}

